﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TinhHuyenXa.Models.Entity;

namespace TinhHuyenXa.Controllers
{
    public class DowdownController : Controller
    {
        //
        // GET: /Dowdown/

        Model1 db = new Model1();
        public ActionResult Index()
        {
            ViewBag.tinh = new SelectList(db.Tinhs, "Id", "Name");
            return View();
        }
        public JsonResult Huyen(long id)
        {
            var huyen = from a in db.Huyens
                        where (a.Tinhid == id)
                        select a;
            return Json(new SelectList(huyen.ToArray(), "Id", "Name"), JsonRequestBehavior.AllowGet);

        }
        public JsonResult xa (long id)
        {
            var xa = from b in db.Xas
                     where b.Id == id
                     select b;
            return Json(new SelectList(xa.ToArray(), "Id", "Name"), JsonRequestBehavior.AllowGet);

        }
        public IList<Huyen> gethuyen(long  tinhid)
        {
            return db.Huyens.Where(m => m.Tinhid == tinhid).ToList();

        }
         [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadlacssByTinh(string tinhname)
        {
            var huyenlist = this.gethuyen(Convert.ToInt64(tinhname));
            var huyendate = huyenlist.Select(m => new SelectListItem()
            {
                Text= m.Name,
                Value = m.Tinhid.ToString(),
            });
            return Json(huyendate, JsonRequestBehavior.AllowGet);
        }
	}
}